<?php

namespace app\admin\model;

use think\Model;
use app\admin\model\Country as CountryModel;

class DataSource extends Model
{





    // 表名
    protected $name = 'data_source';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'online_text',
        'status_text'
    ];


    protected static function init()
    {
        self::afterInsert(function ($row) {
            $pk = $row->getPk();
            $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
        });
    }


    public function getOnlineList()
    {
        return ['1' => __('Online 1'), '0' => __('Online 0')];
    }

    public function getStatusList()
    {
        return ['1' => __('Status 1'), '0' => __('Status 0')];
    }


    public function getOnlineTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['online']) ? $data['online'] : '');
        $list = $this->getOnlineList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function Material()
    {
        return $this->belongsTo('Material', 'material_id', 'id');
    }

    public function getCountryList()
    {
        $countryModel = new CountryModel();
        $countrys = $countryModel->field('id,name')->select();

        $arr = [];
        foreach ($countrys as $key => $value) {
            $arr[$value['id']] = $value['name'];
        }

        return $arr;
    }
}
